package com.example.system.class03;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @Date 2022/7/18
 * @Created by Jonathan
 */
public class Code08_KM {


//    public static void main(String[] args) {
//        int[] arr = new int[]{3, 3, 3, 4, 4, 4, 39, 39, 9, 9, 9, 100, 100, 100, 200, 200, 200};
//        System.out.println(onlyKTimesNum(arr, 2, 3));
//    }


    public static int onlyKTimesNum(int[] arr, int k, int m) {
        int[] bits = new int[32];
        for (int num : arr) {
            for (int i = 0; i < 32; i++) {
                // i位置是1 不是1的话就是0  直接加上不影响
                bits[i] += ((num >> i) & 1);
            }
        }
        int ans = 0;
        for (int i = 0; i < 32; i++) {
            // 说明i位置是1
            if (bits[i] % m != 0) {
                ans |= (1 << i);
            }
        }
        return ans;
    }

    // for test
    public static int test(int[] arr, int k, int m) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int num : arr) {
            if (map.containsKey(num)) {
                map.put(num, map.get(num) + 1);
            } else {
                map.put(num, 1);
            }
        }
        for (Integer num : map.keySet()) {
            if (map.get(num) == k) {
                return num;
            }
        }
        return -1;
    }

    public static int[] randomArray(int maxKinds, int range, int k, int m) {
        int ktimeNum = randomNumber(range);
        int numKinds = (int) (Math.random() * maxKinds) + 2;
        int[] arr = new int[k + (numKinds - 1) * m];
        int index = 0;
        for (; index < k; index++) {
            arr[index] = ktimeNum;
        }
        numKinds--;
        Set<Integer> set = new HashSet<>();
        set.add(ktimeNum);
        while (numKinds != 0) {
            int curNum = 0;
            do {
                curNum = randomNumber(range);
            } while (set.contains(curNum));
            set.add(curNum);
            numKinds--;
            for (int i = 0; i < m; i++) {
                arr[index++] = curNum;
            }
        }
        // 到此arr填好了
        // 打乱
        for (int i = 0; i < arr.length; i++) {
            int j = (int) (Math.random() * arr.length);// 0 ~ N-1
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
        return arr;
    }

    // [-range, +range]
    public static int randomNumber(int range) {
        return ((int) (Math.random() * range) + 1) - ((int) (Math.random() * range) + 1);
    }

    public static void main(String[] args) {
        int kinds = 100;
        int range = 200;
        int testTime = 100_0000;
        int max = 9;
        System.out.println("测试开始");
        for (int i = 0; i < testTime; i++) {
            int a = (int) (Math.random() * max) + 1;
            int b = (int) (Math.random() * max) + 1;
            int k = Math.min(a, b);
            int m = Math.max(a, b);
            if (k == m) {
                m++;
            }
            int[] arr = randomArray(kinds, range, k, m);
            int ans1 = test(arr, k, m);
            int ans2 = onlyKTimesNum(arr, k, m);
            if (ans1 != ans2) {
                System.out.println("出错了!");
            }
        }
        System.out.println("测试结束");
    }

}
